import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.logging.Logger;

//小兵
public class Minion extends Unit{
    //不同线路的兵线走的路径不太一样
    MinionLine line; // 0: 上路， 1:中路 2：下路
    int step = 12;

    int removeY = 340;
    int incrementx = 0;
    int incrementy = 0;

    Coord start;

    static ArrayList<Coord> downPathToTargetRedList;
    static ArrayList<Coord> downPathToTargetBlueList;
    int currentDownMoveRedIndex;
    int currentDownMoveIndexBlue;

    Logger logger = MyLogger.getLogger();

    // 0: red, 1: blue
    Team team;

    Minion(Team team, MinionLine line)
    {
        start = new Coord(0,0);
        id = GameFrame.getRedTeamUnitID(team);
        this.line = line;
        this.team = team;
        this.attackRadius = 80;
        this.hpLeft = 300;
        this.hpTotal = 300;
        damage = 10;
        downLineTarget = new Coord(952,707);
        upperLineTarget = new Coord(160,134);

        switch (team)
        {
            case RED: redMinionInit(); break;

            case BLUE: blueMinionInit(); break;
        }

        x = start.x;
        y = start.y;

        state = UnitState.Move;

        //logger.info(this.toString());
    }

    void redUpperMinionInit()
    {
        start = new Coord(160,602);
    }

    void redMiddleLineInit()
    {
        start = new Coord(300,612);
    }

    void redDownMinionInit()
    {
        start = new Coord(346,748);
        logger.info(String.valueOf(Map.data[start.y][start.x]));

        this.movingDirection = MovingDirection.RIGHT;
        if (downPathToTargetRedList == null)
        {
            downPathToTargetRedList = GameFrame.map.pathFindInit(start, downLineTarget);
            Collections.reverse(downPathToTargetRedList);
        }

    }

    void move()
    {
        if (state != UnitState.Move)
        {
            return;
        }

        if (team == Team.RED) //红色队伍
        {
            switch (line)
            {
                case UPPER_LINE: redUpperLineMove();break;

                case MIDDLE_LINE: redMiddleLineMove();break;

                case DOWN_LINE: redDownLineMove();break;
            }
        }
        else if (team == Team.BLUE) //蓝色队伍
        {
            switch (line)
            {
                case UPPER_LINE: blueUpperLineMove();break;

                case MIDDLE_LINE: blueMiddleLineMove();break;

                case DOWN_LINE: blueDownLineMove();break;
            }
        }
    }

    @Override
    void attack(Graphics graphics) {
        if (state != UnitState.Attack)
            return;

        final Unit self = attack.self;
        final Unit target = attack.target;
        int incrementX = target.x - self.x;
        int incrementY = target.y - self.y;
        int ix = incrementX / 20;
        int iy = incrementY / 20;

        //1、画子弹
        int x = self.x + timer1 * ix;
        int y = self.y + timer1 * iy;

        //画子弹
        if (team == Team.RED)
        {
            graphics.setColor(new Color(255, 0,0));
        }
        else if (team == Team.BLUE)
        {
            graphics.setColor(new Color(0,0, 255));
        }

        //logger.info(self.x + " " + self.y + " " + target.x + " " + target.y + " x,y: " + x + " " + y + " timer: " + timer1);

        timer1++;

        if (timer1 < 20)
        {
            graphics.fillRect(x, y, 2, 2);

            //2、对方减血量
            if (Math.abs(x - target.x) < 17 || Math.abs(y - target.y) < 17)
            {
                timer1 = 0;
                target.hpLeft -= (int) (damage + Math.random() * 5); //攻击力是5-10
                if (target.hpLeft <= 0)
                {
                    state = UnitState.Move;
                }
            }
        }
        else
            return;


    }

    @Override
    public String toString() {
        return  super.toString() + "Minion{" +
                "line=" + line +
                ", step=" + step +
                ", removeY=" + removeY +
                ", timer1=" + timer1 +
                ", start=" + start +
                ", currentDownMoveIndexRed=" + currentDownMoveRedIndex +
                ", currentDownMoveIndexBlue=" + currentDownMoveIndexBlue +
                ", team=" + team +
                '}';
    }

    void paint(Graphics graphics)
    {
        //logger.info(x + ": " + y);
        int x = this.x;
        int y = this.y;

        //画玩家血条
        graphics.setColor(new Color(255 - hpLeft * 255 / hpTotal , hpLeft * 255 / hpTotal, 0));
        graphics.fillRect(x, y, hpLeft * 100 / hpTotal, 10);
        y += 10;
        graphics.setColor(new Color(0 , 255, 0));
        graphics.fillRect(x, y, 140, 10);

        y+=10;
        //画玩家蓝条
        graphics.setColor(new Color(0,0,255));
        graphics.drawRect(x, y, 40, 10);
        graphics.fillRect(x, y, 30, 10);

        graphics.setColor(new Color(0, 0,0 ));
        graphics.drawString("小兵(" +x + "," + y + "), hpLeft: " + hpLeft,x,y);

        if (team == Team.RED)
        {
            graphics.setColor(new Color(255, 0, 0));
            //绘制路径
            if (downPathToTargetRedList != null)
            {
                for(Coord coord: downPathToTargetRedList)
                {
                    graphics.fillRect(coord.x, coord.y, 3,3);
                }
            }

            for (int i = 0; i < Map.height; i++) {
                for (int j = 0; j < Map.width; j++) {
                    if (Map.data[i][j] != MapUnit.IDLE && Map.data[i][j] != MapUnit.BAR )
                        graphics.fillRect(i, j, 3,3);
                }
            }
        }

        if (team == Team.BLUE)
        {
            if (downPathToTargetBlueList != null)
            {
                graphics.setColor(new Color(0, 0,255 ));
                for(Coord coord: downPathToTargetBlueList)
                {
                    graphics.fillRect(coord.x, coord.y, 3,3);
                }
            }
        }

        //画小兵，现在是画一个小球
        graphics.fillArc(x, y, 30,30,180,360);
        y+=30;
        graphics.fillRect(x,y,30,20);
        y+=10;
        graphics.fillRect(x,y,10,20);
        graphics.fillRect(x + 20,y,10,20);
    }

    void redMinionInit()
    {
        switch (line)
        {
            case UPPER_LINE: redUpperMinionInit(); break;

            case MIDDLE_LINE: redMiddleLineInit(); break;

            case DOWN_LINE: redDownMinionInit(); break;
        }
    }

    void redMiddleLineMove()
    {
        // 5984 x 4452
        if (x < 896)
        {
            x += step * GameFrame.windowWidth / GameFrame.windowHeight;
            y -= step;
        }
    }

    void redDownLineMove()
    {
        if (state != UnitState.Move || downPathToTargetRedList == null)
        {
            return;
        }

        if (currentDownMoveRedIndex < downPathToTargetRedList.size())
        {
            Coord coord = downPathToTargetRedList.get(currentDownMoveRedIndex++);

            //logger.info(coord.y + " " + coord.x + " "  + Map.data[coord.y][coord.x]);
            if (Map.data[coord.y + incrementy][coord.x] == MapUnit.IDLE) //将要走的位置可以走
            {
                //新走一步原来的路要设置为空，其他角色才可以走
                Map.data[y + incrementy][x] = MapUnit.IDLE;

                x = coord.x;
                y = coord.y + incrementy;
                Map.data[coord.y + incrementy][coord.x] = MapUnit.MINION;
            }
            else if (movingDirection == MovingDirection.RIGHT)//不能走
            {
                x = coord.x ;
                incrementy = (int) ((Math.random() - 0.5) * 3 * 30);//如果是在往右边走，则随机把Y的值上下移动一点
                y = coord.y + incrementy;
            }
        }
        else
        {
            y -= step;
        }
    }

    void redUpperLineMove()
    {
        if (y > upperLineTarget.y)
        {
            y -= step;
        }
        else
        {
            x += step;
        }
    }

    void blueUpperMinionInit()
    {
        start = new Coord(772,134);
    }

    void blueMiddleLineInit()
    {
        start = new Coord(782,274);
    }

    void blueDownMinionInit()
    {
        start = new Coord(1006, 282); //1600,3611
        movingDirection = MovingDirection.DOWN;
        if (downPathToTargetBlueList == null)
        {
            downPathToTargetBlueList = GameFrame.map.pathFindInit(start, downLineTarget);
            Collections.reverse(downPathToTargetBlueList);
        }
    }

    void blueMinionInit()
    {
        switch (line)
        {
            case UPPER_LINE: blueUpperMinionInit(); break;

            case MIDDLE_LINE: blueMiddleLineInit(); break;

            case DOWN_LINE: blueDownMinionInit(); break;
        }
    }

    void blueMiddleLineMove()
    {
        // 5984 x 4452
        //if (x > 296)
        {
            x -= step * 5984 / 4452;
            y += step;
        }
    }

    void blueDownLineMove() //1600,3611
    {
        if (state != UnitState.Move || downPathToTargetBlueList == null)
        {
            return;
        }

        if (currentDownMoveIndexBlue < downPathToTargetBlueList.size())
        {
            final Coord coord = downPathToTargetBlueList.get(currentDownMoveIndexBlue++);
            if (Map.data[coord.y][coord.x + incrementx] == MapUnit.IDLE) //将要走的位置可以走
            {
                //新走一步原来的路要设置为空，其他角色才可以走
                Map.data[coord.y][coord.x + incrementx] = MapUnit.IDLE;
                x = coord.x + incrementx;
                y = coord.y;
                Map.data[coord.y][coord.x + incrementx] = MapUnit.MINION;
            }
            else if (movingDirection == MovingDirection.DOWN)//不能走
            {
                incrementx = (int) ((Math.random() - 0.5) * 3 * 30);
                x = coord.x + incrementx;
                y = coord.y;
            }
        }
        else
        {
            x -= step;
        }
    }

    void blueUpperLineMove()
    {
        if (x > upperLineTarget.x)
        {
            x -= step;
        }
        else
        {
            y += step;
        }
    }
}
